package src
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class User extends MovieClip
	{
		private var myAvatar:String;
		private var myUsername:String;
		private var myPoints:uint;
		private var myLevel:uint;
		private var fib:Array = new Array;
		
		private var pointsBar:Sprite = new Sprite();;
		
		public static var h:Number;

		public function User(){
			fib = createFib(10);
			h = this.height;
			this['plus'].addEventListener(MouseEvent.CLICK,onClick);
			this['minus'].addEventListener(MouseEvent.CLICK,onClick);
		}
		private function onClick(e:MouseEvent):void{
			switch (e.currentTarget) {
				case plus: addPoints(); break;
				case minus: decPoints(); break;
			}
		}

		public function fill():void{
			updateProgress();			
			this["avatarImg"].gotoAndStop(avatar);
			this["nameTxt"].text = username;
		}
		
		private function updateProgress():void{
			pointsFrame.gotoAndStop(level);
			pointsLines.gotoAndStop(level);
			
			pointsBar.graphics.clear();
			pointsBar.graphics.beginFill(0x666666);
			pointsBar.graphics.drawRect(0,0,this.width,-pointsFrame.height*points/fib[level]);
			pointsBar.graphics.endFill();
			pointsBar.y = pointsFrame.y;
			
			pointsBar.mask = pointsFrame;
			addChildAt(pointsBar,1);
		}

		public function addPoints():void{
			points++;
			if (points == fib[level]){
				points = 0;
				level ++;
			}
			updateProgress();
		}

		public function decPoints():void{
			if (points != 0) {
				points--;
			}
			updateProgress();
		}

		private function createFib(n:uint):Array{
			var arr:Array = new Array(1,1);
			for (var i:uint = 2; i<n; i++){
				arr[i] = arr[i-1] +arr[i-2];
			}
			return arr;
		}
		
		public function get points():uint
		{
			return myPoints;
		}

		public function set points(value:uint):void
		{
			myPoints = value;
		}

		public function get level():uint
		{
			return myLevel;
		}

		public function set level(value:uint):void
		{
			myLevel = value;
		}

		public function get username():String
		{
			return myUsername;
		}

		public function set username(value:String):void
		{
			myUsername = value;
		}

		public function get avatar():String
		{
			return myAvatar;
		}

		public function set avatar(value:String):void
		{
			myAvatar = value;
		}


	}
}